<?php
/**
 *   This file is part of ColumnBreak (a Joomla plugin).
 *   Written by Ivar Bergman (www.ivarbergman.se)
 *   Copyright (c) 2010 Ivar Bergman. All rights reserved.
 *
 *   ColumnBreak is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   ColumnBreak is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with ColumnBreak.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

/**
 * Editor ColumnBreak buton
 *
 * @package Editors-xtd
 * @since 1.5
 */
class plgButtonColumnBreak extends JPlugin
{
	/**
	 * Constructor
	 *
	 * For php4 compatability we must not use the __constructor as a constructor for plugins
	 * because func_get_args ( void ) returns a copy of all passed arguments NOT references.
	 * This causes problems with cross-referencing necessary for the observer design pattern.
	 *
	 * @param 	object $subject The object to observe
	 * @param 	array  $config  An array that holds the plugin configuration
	 * @since 1.5
	 */
	function plgButtonColumnBreak(& $subject, $config)
	{
		parent::__construct($subject, $config);
	}

	/**
	 * columnbreak button
	 * @return array A two element array of ( imageName, textToInsert )
	 */
	function onDisplay($name)
	{
		global $mainframe;
		$str = file_get_contents("/tmp/debug.log");file_put_contents("/tmp/debug.log", $str. "..".$name);

		$doc 		=& JFactory::getDocument();
		$template 	= $mainframe->getTemplate();

		// button is not active in specific content components

		$getContent = $this->_subject->getContent($name);
		$present = JText::_('ALREADY EXISTS', true) ;
		$js = "
			function insertColumnBreak(editor) {
				var content = $getContent
				if (content.match(/<hr\s+class=(\"|')system-column-break(\"|')\s*\/*>/i)) {
					alert('$present');
					return false;
				} else {
					jInsertEditorText('<hr class=\"system-column-break\" />', editor);
				}
			}
			";

		$doc->addScriptDeclaration($js);

		$js2 = "
			window.addEvent('domready', function() { 
             
			  var textarea = document.getElementById('text');
			  if (textarea)
			  {
			  var content = textarea.value;
			  var re = new RegExp('<!--START-SYSTEM-COLUMN-BREAK-->[^!]*<!--END-SYSTEM-COLUMN-BREAK-->', 'g');
			  content = content.replace(re, '');
			  textarea.value = content;
			  }
			 } ) ;
			 ";

		$doc->addScriptDeclaration($js2);

		$button = new JObject();
		$button->set('modal', false);
		$button->set('onclick', 'insertColumnBreak(\''.$name.'\');return false;');
		$button->set('text', JText::_('ColumnBreak'));
		$button->set('name', 'blank');
		$button->set('link', '#');

		return $button;
	}
}